package com.fimuz.mqmeet.api.login.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fimuz.mqmeet.api.login.dao.LoginDao;
import com.fimuz.mqmeet.api.login.service.LoginService;
import com.fimuz.mqmeet.api.login.service.model.SubmitSpModel;
import com.fimuz.mqmeet.api.login.service.model.SubmitSrModel;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzLoginuserMapper;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzTeamMapper;
import com.fimuz.mqmeet.helper.mybatis.model.HxzLoginuser;
import com.fimuz.mqmeet.helper.mybatis.model.HxzLoginuserExample;
import com.fimuz.mqmeet.helper.mybatis.model.HxzTeam;
import com.fimuz.mqmeet.helper.mybatis.model.HxzTeamExample;
import com.fimuz.mqmeet.helper.utils.PasswordUtils;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.utils.ExtAsserts;

@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class LoginServiceImpl extends CoBaseService implements LoginService {
	@Resource
	private LoginDao loginDao;

	@Resource
	private HxzLoginuserMapper loginMapper;
	@Resource
	private HxzTeamMapper teamMapper;
	
	
	@Override
	public SubmitSrModel submit(SubmitSpModel spModel) {
		ExtAsserts.notNull(spModel.getUserName(),"user_name");
		ExtAsserts.notNull(spModel.getPassword(),"password");
		SubmitSrModel srModel = new SubmitSrModel();
		HxzLoginuserExample hxzLoginuserExample = new HxzLoginuserExample();
		HxzLoginuserExample.Criteria criteria = hxzLoginuserExample.createCriteria();
		criteria.andIsDeletedEqualTo("0");
		criteria.andUserEmailEqualTo(spModel.getUserName().toLowerCase());
		List<HxzLoginuser> userList = loginMapper.selectByExample(hxzLoginuserExample);

		if(userList.size()>0){
			HxzLoginuser user = userList.get(0);
			if(PasswordUtils.mi(spModel.getPassword()).equals(user.getLoginPassword())){
				srModel.setLoginResult("1");
				srModel.setUserId(user.getId());
				srModel.setUserName(user.getUserName());
			}else{
				srModel.setLoginResult("-1");
			}
		}
		
		return srModel;
	}

}
